package com.yjf.ljr.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.yjf.ljr.R;
import com.yjf.ljr.activity.ProjectDetailActivity;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.InvestDetailDataClass.ProjectInfo;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.view.ProgressWheelView;

public class InvestDetailAdapter extends BaseAdapter {

	private Context mContext;
	private List<ProjectInfo> mData;
	private LayoutInflater mInflater;
	protected ImageLoader imageLoader;
	DisplayImageOptions options;

	public InvestDetailAdapter(Context ctx, List<ProjectInfo> data) {
		mContext = ctx;
		mData = data;
		mInflater = LayoutInflater.from(ctx);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(ctx));
		options = new DisplayImageOptions.Builder().showStubImage(Color.parseColor("#000000"))
				.showImageForEmptyUri(Color.parseColor("#000000")).showImageOnFail(Color.parseColor("#000000"))
				.cacheInMemory(true).cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@TargetApi(Build.VERSION_CODES.LOLLIPOP)
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.item_invest_detail, null);
			holder = new ViewHolder();
			holder.llInvest = (LinearLayout) convertView.findViewById(R.id.llInvestDetailInvestBg);
			holder.ivLock = (ImageView) convertView.findViewById(R.id.ivInvestDetailLock);
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tvInvestDetailTitle);
			holder.tvStatus = (TextView) convertView.findViewById(R.id.tvInvestDetailStatus);
			holder.tvNeedMoney = (TextView) convertView.findViewById(R.id.tvInvestDetailNeedMoney);
			holder.tvCompanyName = (TextView) convertView.findViewById(R.id.tvInvestDetailCompanyName);
			holder.tvYearGet = (TextView) convertView.findViewById(R.id.tvInvestDetailYearGet);
			holder.tvMoneyTime = (TextView) convertView.findViewById(R.id.tvInvestDetailMoneyTime);
			holder.tvShowProgress = (TextView) convertView.findViewById(R.id.tvShowProgress);
			holder.tvTypeName = (TextView) convertView.findViewById(R.id.tvTypeName);
			holder.proWheelView = (ProgressBar) convertView.findViewById(R.id.progressBarInvest);
			holder.ivTypeLogo = (ImageView) convertView.findViewById(R.id.ivTypeLogo);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			resetHolder(holder);
		}
		final ProjectInfo item = mData.get(position);
		if ("1".equals(item.isDirectional)) {
			holder.ivLock.setVisibility(View.VISIBLE);
		} else {
			holder.ivLock.setVisibility(View.GONE);
		}

		/** 名称 **/
		AppUtil.setViewText(holder.tvTitle, item.loanName);

		/** 状态 **/
		if (!TextUtils.isEmpty(item.status)) {
			if ("1".equals(item.status)) {
				holder.llInvest.setBackgroundColor(Color.parseColor("#03b888"));
				holder.tvStatus.setText("参加支持");
				holder.tvYearGet.setTextColor(mContext.getResources().getColor(R.color.orange_text_content));
				holder.tvMoneyTime.setTextColor(mContext.getResources().getColor(R.color.darkgrey));
				holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.ca0d861));
				holder.tvStatus.setBackground(mContext.getDrawable(R.drawable.green_shape));
			} else if ("2".equals(item.status)) {
				holder.llInvest.setBackgroundColor(Color.parseColor("#006f41"));
				holder.tvStatus.setText("投资已满");
				holder.tvYearGet.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvMoneyTime.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.darkgrey));
				holder.tvStatus.setBackground(mContext.getDrawable(R.drawable.gray_shape));
			} else if ("3".equals(item.status)) {
				holder.llInvest.setBackgroundColor(Color.parseColor("#b4b4b4"));
				holder.tvStatus.setText("未到时间");
				holder.tvYearGet.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvMoneyTime.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.darkgrey));
				holder.tvStatus.setBackground(mContext.getDrawable(R.drawable.gray_shape));
			} else if ("4".equals(item.status)) {
				holder.llInvest.setBackgroundColor(Color.parseColor("#00819e"));
				holder.tvStatus.setText("还款完成");
				holder.tvYearGet.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvMoneyTime.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.darkgrey));
				holder.tvStatus.setBackground(mContext.getDrawable(R.drawable.gray_shape));
			} else if ("5".equals(item.status) || "7".equals(item.status)) {
				holder.llInvest.setBackgroundColor(Color.parseColor("#07b9c3"));
				holder.tvStatus.setText("正在还款");
				holder.tvYearGet.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvMoneyTime.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.darkgrey));
				holder.tvStatus.setBackground(mContext.getDrawable(R.drawable.gray_shape));
			} else if ("6".equals(item.status)) {
				holder.llInvest.setBackgroundColor(Color.parseColor("#00839F"));
				holder.tvStatus.setText("已过投资时间");
				holder.tvYearGet.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvMoneyTime.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.darkgrey));
				holder.tvStatus.setBackground(mContext.getDrawable(R.drawable.gray_shape));
			} else if ("8".equals(item.status)) {
				holder.llInvest.setBackgroundColor(Color.parseColor("#808080"));
				holder.tvStatus.setText("交易失败");
				holder.tvYearGet.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvMoneyTime.setTextColor(mContext.getResources().getColor(R.color.gray_text_nine));
				holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.darkgrey));
				holder.tvStatus.setBackground(mContext.getDrawable(R.drawable.gray_shape));
			}
		}
		// //////////////////////

		String tempBorrowMoney = item.loanAmount;
		/** 借款金额 **/
		if (!TextUtils.isEmpty(tempBorrowMoney)) {
			double borrowMoney = 0;
			try {
				borrowMoney = Double.parseDouble(tempBorrowMoney.replace(",", ""));
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (borrowMoney < 10000) {
				int index = tempBorrowMoney.indexOf(".");
				String showBorrow = "";
				String decimalPoint = "";
				if (-1 != index) {
					decimalPoint = tempBorrowMoney.substring(index + 1, tempBorrowMoney.length());

				}
				if (!TextUtils.isEmpty(decimalPoint)) {
					int pointValue = Integer.parseInt(decimalPoint);
					if (pointValue > 0) {
						showBorrow = tempBorrowMoney;
					} else {
						showBorrow = tempBorrowMoney.substring(0, index).replace(",", "");
					}
				} else {
					showBorrow = tempBorrowMoney.substring(0, index);
				}
				holder.tvNeedMoney.setText(showBorrow + "元");

			} else {
				int tenThousand = (int) borrowMoney / 10000;
				double thousand = (borrowMoney % 10000) / 10000d;
				if (thousand > 0) {
					holder.tvNeedMoney.setText((tenThousand + thousand) + "万");
				} else {
					holder.tvNeedMoney.setText(tenThousand + ".00" + "万");
				}
			}

		}

		// //////////////////

		// /** 借款金额 **/
		// if (!TextUtils.isEmpty(item.loanAmount)) {
		// float borrowMoney = 0;
		// try {
		// borrowMoney = Float.parseFloat(item.loanAmount.replace(",", ""));
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// int tenThousand = (int) borrowMoney / 10000;
		// int thousand = (int) borrowMoney % 10000;
		// if (thousand > 0) {
		// holder.tvNeedMoney.setText(tenThousand + "." + (thousand / 1000) +
		// "万");
		// } else {
		// holder.tvNeedMoney.setText(tenThousand + "万");
		// }
		// }

		/** 投资时间 **/
		if (!TextUtils.isEmpty(item.investAvlDate)) {
			holder.tvCompanyName.setText("起投时间：" + item.investAvlDate);
		}

		/** 年化收益 **/
		if (!TextUtils.isEmpty(item.interestRate)) {
			holder.tvYearGet.setText(item.interestRate);
		}

		/** 融资期限 **/
		if (!TextUtils.isEmpty(item.timeLimit)) {
			holder.tvMoneyTime.setText(item.timeLimit);
		}

		/** 进度条 **/

		if (!TextUtils.isEmpty(item.investProgress)) {
			double progress = 0;
			String temp = "";
			String temp2 = "";
			DecimalFormat df = new DecimalFormat("#.00");

			try {
				String strProgress = item.investProgress;
				if (strProgress.endsWith("%")) {
					strProgress = strProgress.substring(0, strProgress.length() - 1);
				}
				progress = Double.parseDouble(strProgress);
			} catch (Exception e) {
				e.printStackTrace();
			}
			/*
			 * if ("2".equals(item.state)) {
			 * holder.proWheelView.setBarColor(Color.parseColor("#b4b4b4")); }
			 * if ("3".equals(item.state)) {
			 * holder.proWheelView.setBarColor(Color.parseColor("#b4b4b4")); }
			 */
			/*
			 * if ((100 == (int) progress)) {
			 * holder.proWheelView.setBarColor(Color.parseColor("#b4b4b4"));
			 * 
			 * }
			 */

			holder.proWheelView.setProgress((int) progress);

			temp = df.format(progress);
			if ("0".equals("" + temp.indexOf("."))) {
				temp = "0" + temp;
			}
			if (!("-1".equals(temp.indexOf(".")))) {
				String aaString = temp.substring(temp.indexOf(".") + 1, temp.length());
				if ("0".equals(temp.substring(temp.indexOf(".") + 1, temp.length()))) {
					temp2 = temp.substring(0, temp.indexOf("."));
				} else {
					temp2 = temp;
				}
			} else {
				temp2 = temp;
			}
			holder.tvShowProgress.setText((temp2) + "%");

		}

		/*** 加载标类型 ****/
		if (!TextUtils.isEmpty(item.typeLogo) && !"未配置".equals(item.typeLogo) && !TextUtils.isEmpty(item.typeName)
				&& !"未配置".equals(item.typeName)) {
			// PicLoad.getImage(holder.ivTypeLogo, null, item.typeLogo);
			holder.ivTypeLogo.setVisibility(View.VISIBLE);
			holder.tvTypeName.setText(item.typeName);
			imageLoader.displayImage(item.typeLogo, holder.ivTypeLogo, options);
		} else {
			holder.tvTypeName.setText("");
			holder.ivTypeLogo.setVisibility(View.GONE);
		}
		/** 设置跳转详情 **/
		if (!TextUtils.isEmpty(item.demandId)) {
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					Intent i = new Intent(mContext, ProjectDetailActivity.class);
					Bundle b = new Bundle();
					b.putSerializable("item", item);
					i.putExtra("bundle", b);
					i.putExtra("Statusdetail", item.status);

					// i.putExtra("demandId", item.demandId);// 项目ID
					// // SPreferences.saveData(mContext, item.demandId,
					// // "projectId");
					// i.putExtra("isDirectional", item.isDirectional);// 是否定向投资
					// // SPreferences.saveData(mContext, item.isDirectional,
					// // "directional");
					// /*** 年利化率 **/
					// i.putExtra("interestRate", item.interestRate);
					// // SPreferences.saveData(mContext, item.interestRate,
					// // "yearRate");
					// /*** 期限 **/
					// i.putExtra("timeLimit", item.timeLimit);
					// // SPreferences.saveData(mContext, item.timeLimit,
					// // "timeLimit");
					// /*** 起投时间 **/
					// i.putExtra("investAvlDate", item.investAvlDate);
					// // SPreferences.saveData(mContext, item.investAvlDate,
					// // "startTimeInvestDetail");
					((Activity) mContext).startActivityForResult(i, CommonData.REQUEST_CODE_LOGIN);
				}
			});
		}

		return convertView;
	}

	private class ViewHolder {
		LinearLayout llInvest;
		ImageView ivLock;
		TextView tvTitle;
		TextView tvStatus;
		TextView tvNeedMoney;
		TextView tvCompanyName;
		TextView tvYearGet;
		TextView tvMoneyTime;
		TextView tvShowProgress;
		TextView tvTypeName;
		ImageView ivTypeLogo;
		ProgressBar proWheelView;
	}

	private void resetHolder(ViewHolder holder) {
		holder.tvStatus.setText(null);
		holder.tvNeedMoney.setText(null);
		holder.tvCompanyName.setText(null);
		holder.tvYearGet.setText(null);
		holder.tvMoneyTime.setText(null);
		holder.tvShowProgress.setText(null);
	}
}
